import 'package:flutter/cupertino.dart';

class TurnBox extends StatefulWidget {

  const TurnBox({Key? key, this.turns, this.speeds, required this.child}) : super(key: key);

  final double? turns;
  final double? speeds;

  final Widget child;

  @override
  State createState() => TurnBoxState();

}

class TurnBoxState extends State<TurnBox> with SingleTickerProviderStateMixin {

  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this,
        lowerBound: -double.infinity,
        upperBound: double.infinity
    );
    _controller.value = widget.turns!;
  }

  @override
  Widget build(BuildContext context) {
     return RotationTransition(
         turns: _controller,
         child: widget.child,
     );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  void didUpdateWidget(covariant TurnBox oldWidget) {
     super.didUpdateWidget(oldWidget);
     //旋转角度发生变化时  执行过度动画
     if (oldWidget.turns != widget.turns) {
       _controller.animateTo(
         widget.turns!,
         duration: const Duration(microseconds: 200),
         curve: Curves.easeOut
       );
     }
  }

}